import java.util.Random;

import static jdk.nashorn.internal.objects.NativeMath.random;

public class Demo18 {
	private static int ticket=1000;
	private static Object locker1=new Object();
	private static Object locker2=new Object();
	public static void main(String[] args) throws InterruptedException {
		Thread t1=new Thread(()->{
			while(true) {
				synchronized (locker1) {
					if (ticket > 0) {
						try {
							locker1.wait();
							Thread.sleep(1000);
						} catch (InterruptedException e) {
							throw new RuntimeException(e);
						}
						if(ticket==0) {
							System.out.println("卖票结束");
							break;
						}
						System.out.println(Thread.currentThread().getName() + " 窗口0 " + --ticket);
					} else {
						break;
					}
				}
				synchronized (locker2) {
					locker2.notify();
				}
			}
		});
		Thread t2=new Thread(()->{
			while(true) {
				synchronized (locker2) {
					if (ticket > 0) {
						try {
							locker2.wait();
							Thread.sleep(1000);
						} catch (InterruptedException e) {
							throw new RuntimeException(e);
						}
						if(ticket==0) {
							System.out.println("卖票结束");
							break;
						}
						System.out.println(Thread.currentThread().getName() + " 窗口1 " + --ticket);
					} else {
						break;
					}
				}
				synchronized (locker1) {
					locker1.notify();
				}
			}
		});
		t1.start();
		t2.start();
		Thread.sleep(1000);
		synchronized (locker1) {
			locker1.notify();
		}
		//System.out.println("卖票结束");
	}
}
